package cn.javabb.core.service.sys;

import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseService;
import cn.javabb.core.constant.SysConstant;
import cn.javabb.core.entity.sys.RoleDO;
import cn.javabb.core.entity.sys.UsersRolesDO;
import cn.javabb.core.mapper.sys.RoleMapper;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class RoleService extends BaseService<RoleDO> {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UsersRolesService usersRolesService;

    @Autowired
    private MenuService menuService;
    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 查询用户的角色
     * 
     * @author QINB
     * @param userId
     * @return
     */
    public List<RoleDO> listByUserId(Integer userId) {
        JSONObject param = new JSONObject();
        param.put("sqlid", RoleMapper.class.getName() + ".listByUserId");
        param.put("userId", userId);
        List<RoleDO> roleList = this.queryList(param);
        return roleList;
    }

    /**
     * 保存用户角色
     * 
     * @author QINB
     * @param userId
     * @return
     */
    public int saveUsersRoles(Integer userId, String roleIds) {
        // List<String> roleId = ListUtils.stringToListString(roleIds);
        int num = 0;
        String[] idArr = roleIds.split(",");
        if(idArr==null || (idArr!=null && idArr.length==0)){
            return 0;
        }
        for (String id : idArr) {
            UsersRolesDO ur = new UsersRolesDO();
            ur.setUserId(userId);
            ur.setRoleId(Integer.valueOf(id));
            num += usersRolesService.insertSelective(ur);
        }
        return num;
    }

    /**
     * 更新
     * 
     * @author QINB
     * @param userId
     * @param roleIds
     * @return
     */
    public int updateUsersRoles(Integer userId, String roleIds) {
        delUsersRoles(userId);
        return saveUsersRoles(userId, roleIds);
    }

    /**
     * 删除用户角色
     * 
     * @author QINB
     * @param userId
     * @return
     */
    public int delUsersRoles(Integer userId) {
        UsersRolesDO ur = new UsersRolesDO();
        ur.setUserId(userId);
        return usersRolesService.deleteByWhere(ur);
    }

    /*判断是不是超级管理员*/
    public boolean isAdminRole(String ids) {
        Example ex = new Example(RoleDO.class);
        ex.createCriteria().andIn("id", ListUtils.stringToListString(ids));
        List<RoleDO> list = this.queryListByExample(ex);
        for (RoleDO role : list) {
            if (SysConstant.ADMIN_ROLE_CODE.equals(role.getRoleCode())) {
                return true;
            }
        }
        return false;
    }
    /*删除角色,并删除角色下面的权限*/
    @Transactional(rollbackFor=Exception.class)
    public boolean delete(String ids) {
        List<String> idList = ListUtils.stringToListString(ids);
        int num = 0;
        for (String id : idList) {
            // 删除角色
            num += this.deleteById(id);
            // 删除角色权限
            rolePermissionService.deleteByRoleId(Integer.valueOf(id));
            // 删除用户角色
            usersRolesService.deleteByRoleId(Integer.valueOf(id));

        }
        return num>0;
    }
}
